package com.bravo.base.service;

import com.bravo.base.entity.Label;
import com.bravo.base.respository.LabelRespository;
import com.bravo.common.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 标签业务逻辑类
 */
@Service
public class LabelService {
    @Autowired
    private LabelRespository labelRespository;

    @Autowired
    private IdWorker idWorker;

    /**
     *查询全部标签
     * @return
     */
    public List<Label> findAll(){
        return  labelRespository.findAll();
    }

    /**
     * 根据ID查询标签
     * @param id
     * @return
     */
    public Label findById(String id){
        return labelRespository.findById(id).get();
    }

    /**
     * 增加标签
     * @param label
     */
    public void add(Label label){
        //设置ID
        label.setId(idWorker.nextId()+"");
        labelRespository.save(label);
    }

    /**
     * 修改标签
     * @param label
     */
    public void update(Label label){
        labelRespository.save(label);
    }

    /**
     * 删除标签
     * @param id
     */
    public void deleteById(String id){
        labelRespository.deleteById(id);
    }

    private Specification<Label> createSpecification(Map searchMap){
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if (searchMap.get("labelName")!=null && !"".equals(searchMap.get("labelName"))){
                    predicateList.add(cb.like(root.get("labelName").as(String.class),"%"+searchMap.get("labelName")+"%"));
                }
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                    predicateList.add(cb.equal(root.get("state").as(String.class),searchMap.get("state")));
                }
                if (searchMap.get("recommend")!=null && !"".equals(searchMap.get("recommend"))){
                    predicateList.add(cb.equal(root.get("recommend").as(String.class),searchMap.get("recommend")));
                }
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Label> findSearch(Map searchMap){
        Specification<Label> specification = createSpecification(searchMap);
        return labelRespository.findAll(specification);
    }

    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findsearch(Map searchMap,int page,int size){
        Specification specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return labelRespository.findAll(specification,pageRequest);
    }



}
